home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CPhitGame.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  8.3 KB  |  259 lines

  1. class CPhitGame extends CMovieClipFresh
  2. {
  3.    var _statReporter;
  4.    var _arrFieldHosts;
  5.    var _field = null;
  6.    var _fieldHost = null;
  7.    var _doneShuffling = false;
  8.    var _minDesiredIterations = 100;
  9.    var _maxDesiredIterations = 1000;
  10.    var _maxShuffleSlideLength = 5;
  11.    var _totalIterations = 0;
  12.    var _bias = 0;
  13.    var _isFieldSolved = false;
  14.    var _iLevel = 0;
  15.    var _iSeedOffset = 0;
  16.    var _gameWidth = 480;
  17.    var _gameHeight = 640;
  18.    function CPhitGame()
  19.    {
  20.       super();
  21.    }
  22.    function FirstFrameInitialize()
  23.    {
  24.       _root._game = this;
  25.       if(_root._iStartLevel)
  26.       {
  27.          this._iLevel = _root._iStartLevel;
  28.       }
  29.       this._statReporter = new CServerStatReporter();
  30.       this._arrFieldHosts = new Array();
  31.       Key.addListener(this);
  32.       this.StartFieldSequence();
  33.    }
  34.    function GetLevel()
  35.    {
  36.       return this._iLevel;
  37.    }
  38.    function GetField()
  39.    {
  40.       return this._field;
  41.    }
  42.    function GetFieldHost()
  43.    {
  44.       return this._fieldHost;
  45.    }
  46.    function StartFieldSequence()
  47.    {
  48.       FreshDebug.Trace("Seeding");
  49.       _root.random.SetSeed(this._iLevel + this._iSeedOffset);
  50.       this.StartBuildingField(this._iLevel);
  51.    }
  52.    function StartBuildingField(iField)
  53.    {
  54.       FreshDebug.Trace("StartBuildingField( " + iField + " )");
  55.       if(iField == undefined)
  56.       {
  57.          iField = this._arrFieldHosts.length;
  58.       }
  59.       var _loc3_ = iField;
  60.       this.attachMovie("Field Host","fieldHost" + _loc3_,_loc3_);
  61.       this._fieldHost = CFieldHost(this["fieldHost" + _loc3_]);
  62.       this._arrFieldHosts[iField] = this._fieldHost;
  63.       this._totalIterations = 0;
  64.       this._field = null;
  65.       this._doneShuffling = false;
  66.       this._isFieldSolved = false;
  67.    }
  68.    function onKeyUp()
  69.    {
  70.       var _loc2_ = Key.getCode();
  71.       FreshDebug.Trace("key up=" + _loc2_);
  72.       if(Key.isDown(17))
  73.       {
  74.          switch(_loc2_)
  75.          {
  76.             case 38:
  77.                this.FinishCurrentLevel();
  78.                break;
  79.             case 90:
  80.                this._field.UndoLastPieceMove();
  81.                break;
  82.             case 89:
  83.                this._field.RedoLastPieceMove();
  84.          }
  85.       }
  86.    }
  87.    function IsUndoAvailable()
  88.    {
  89.       return !this._isFieldSolved && this._field.IsUndoAvailable();
  90.    }
  91.    function IsRedoAvailable()
  92.    {
  93.       return !this._isFieldSolved && this._field.IsRedoAvailable();
  94.    }
  95.    function onEnterFrame()
  96.    {
  97.       super.onEnterFrame();
  98.       if(!this._fieldHost)
  99.       {
  100.          return undefined;
  101.       }
  102.       this._field = CPlayingField(this._fieldHost._field);
  103.       if(this._field == null || !this._field.IsAwake())
  104.       {
  105.          return undefined;
  106.       }
  107.       if(this._field && this._field.WantsBuilding())
  108.       {
  109.          this.BuildPendingField();
  110.       }
  111.       if(!this._doneShuffling)
  112.       {
  113.          this.ShuffleField();
  114.       }
  115.       else
  116.       {
  117.          this.UpdateGame();
  118.       }
  119.    }
  120.    function UpdateGame()
  121.    {
  122.       if(!this._isFieldSolved && this._field.AreAllPiecesWithinTray())
  123.       {
  124.          this.FinishCurrentLevel();
  125.       }
  126.    }
  127.    function GetFieldConfiguration(iLevel)
  128.    {
  129.       if(iLevel < 2)
  130.       {
  131.          return new Object({width:4,height:2,minPieceSize:1,maxPieceSize:4,bias:0.5,minTotalIterations:10,maxTotalIterations:50,maxShuffleSlideLength:4});
  132.       }
  133.       if(iLevel < 8)
  134.       {
  135.          var _loc2_ = (iLevel - 2) / 6;
  136.          return new Object({width:6,height:3,minPieceSize:1,maxPieceSize:5,bias:0.5,minTotalIterations:MathUtil.Lerp(50,100,_loc2_),maxTotalIterations:MathUtil.Lerp(100,150,_loc2_),maxShuffleSlideLength:5});
  137.       }
  138.       if(iLevel < 15)
  139.       {
  140.          _loc2_ = (iLevel - 8) / 7;
  141.          return new Object({width:7,height:4,minPieceSize:1,maxPieceSize:6,bias:0.33,minTotalIterations:MathUtil.Lerp(100,200,_loc2_),maxTotalIterations:MathUtil.Lerp(200,300,_loc2_),maxShuffleSlideLength:3});
  142.       }
  143.       if(iLevel < 30)
  144.       {
  145.          _loc2_ = (iLevel - 15) / 15;
  146.          return new Object({width:8,height:5,minPieceSize:1,maxPieceSize:6,bias:0.25,minTotalIterations:MathUtil.Lerp(200,400,_loc2_),maxTotalIterations:MathUtil.Lerp(400,600,_loc2_),maxShuffleSlideLength:2});
  147.       }
  148.       if(iLevel < 50)
  149.       {
  150.          _loc2_ = (iLevel - 30) / 20;
  151.          return new Object({width:9,height:6,minPieceSize:1,maxPieceSize:6,bias:0.25,minTotalIterations:MathUtil.Lerp(400,800,_loc2_),maxTotalIterations:MathUtil.Lerp(800,1200,_loc2_),maxShuffleSlideLength:2});
  152.       }
  153.       if(iLevel < 100)
  154.       {
  155.          _loc2_ = (iLevel - 50) / 50;
  156.          return new Object({width:10,height:6,minPieceSize:2,maxPieceSize:6,bias:0.2,minTotalIterations:MathUtil.Lerp(1000,2000,_loc2_),maxTotalIterations:MathUtil.Lerp(2000,4000,_loc2_),maxShuffleSlideLength:1});
  157.       }
  158.       return new Object({width:12,height:7,minPieceSize:1,maxPieceSize:6,bias:0.2,minTotalIterations:2000,maxTotalIterations:4000,maxShuffleSlideLength:1});
  159.    }
  160.    function BuildPendingField()
  161.    {
  162.       FreshDebug.Trace("Trying to build");
  163.       var _loc2_ = this.GetFieldConfiguration(this._iLevel);
  164.       this._field.Build(_loc2_.width,_loc2_.height,_loc2_.minPieceSize,_loc2_.maxPieceSize);
  165.       this._doneShuffling = false;
  166.       this._totalIterations = 0;
  167.       this._bias = _loc2_.bias;
  168.       this._minDesiredIterations = _loc2_.minTotalIterations;
  169.       this._maxDesiredIterations = _loc2_.maxTotalIterations;
  170.       this._maxShuffleSlideLength = _loc2_.maxShuffleSlideLength;
  171.       this.ScaleField(this._fieldHost);
  172.       FreshDebug.Assert(!this._field.WantsBuilding(),"!_field.WantsBuilding()");
  173.    }
  174.    function ShufflePerFrame()
  175.    {
  176.       var _loc3_ = 1;
  177.       var _loc2_ = Math.min(8,this._maxDesiredIterations - this._totalIterations);
  178.       return this._field.Shuffle(_loc3_,_loc2_,this._maxShuffleSlideLength,this._bias);
  179.    }
  180.    function ShuffleField()
  181.    {
  182.       FreshDebug.Trace("Shuffling");
  183.       var _loc3_ = 1;
  184.       var _loc2_ = Math.min(8,this._maxDesiredIterations - this._totalIterations);
  185.       this._totalIterations += this.ShufflePerFrame();
  186.       if(!this._field.AreAllPiecesOutsideOfTray())
  187.       {
  188.          if(this._totalIterations > this._maxDesiredIterations)
  189.          {
  190.             this.StartBuildingField(this._iLevel);
  191.          }
  192.       }
  193.       else if(this._totalIterations >= this._minDesiredIterations)
  194.       {
  195.          this._doneShuffling = true;
  196.          this.OnFinishShuffling();
  197.       }
  198.    }
  199.    function OnFinishShuffling()
  200.    {
  201.       this._field.RememberPieceBasePositions();
  202.       this._fieldHost.PlayAppear();
  203.    }
  204.    function OnVictoryDisplayFinished()
  205.    {
  206.       FreshDebug.Trace("OnVictoryDisplayFinished");
  207.       this._fieldHost.PlayDisappear();
  208.       this._fieldHost = null;
  209.       this._field = null;
  210.       this.StartNextLevel();
  211.    }
  212.    function FinishCurrentLevel()
  213.    {
  214.       FreshDebug.Trace("closing level");
  215.       this._isFieldSolved = true;
  216.       this.ReportLevelStatsToServer();
  217.       this._field.PlayVictory();
  218.    }
  219.    function StartNextLevel()
  220.    {
  221.       this._iLevel = this._iLevel + 1;
  222.       FreshDebug.Trace("moving to level " + this._iLevel);
  223.       _root._presentation.StoreLastPlayedLevel(this._iLevel);
  224.       FreshDebug.Trace("Fabricating a new level.");
  225.       this.StartFieldSequence();
  226.    }
  227.    function KillFieldHost(host)
  228.    {
  229.       var _loc2_ = 0;
  230.       while(_loc2_ < this._arrFieldHosts.length)
  231.       {
  232.          if(this._arrFieldHosts[_loc2_] == host)
  233.          {
  234.             this._arrFieldHosts[_loc2_] = null;
  235.             break;
  236.          }
  237.          _loc2_ = _loc2_ + 1;
  238.       }
  239.    }
  240.    function ScaleField(fieldHost)
  241.    {
  242.       var _loc4_ = fieldHost._field._pixelWidth / this._gameWidth;
  243.       var _loc5_ = fieldHost._field._pixelHeight / this._gameHeight;
  244.       var _loc3_ = 1 / _loc5_;
  245.       if(_loc4_ > _loc5_)
  246.       {
  247.          _loc3_ = 1 / _loc4_;
  248.       }
  249.       fieldHost._xscale = fieldHost._yscale = _loc3_ * 100;
  250.       fieldHost._x = (this._gameWidth - fieldHost._field._pixelWidth * _loc3_) * 0.5;
  251.       fieldHost._y = fieldHost._field._tableauPixelHeight * _loc3_;
  252.       fieldHost._field.SizeFrame(_loc3_);
  253.    }
  254.    function ReportLevelStatsToServer()
  255.    {
  256.       this._statReporter.ReportLevelStats(this._iLevel,this._field.GetTimeTaken(),this._field.GetNumMovesMade());
  257.    }
  258. }
  259.